﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DGIS.SysSetting.Core.Model;

namespace DGIS.SysSetting.Core.Service.Win
{
    public class EnvVariablesService
    {
        public List<EnvVariables> Get()
        {
            List<EnvVariables> items=new List<EnvVariables>();

            IDictionary dic= Environment.GetEnvironmentVariables();
            ICollection collection= dic.Keys;
            foreach (object obj in collection)
            {
                string name = obj.ToString();
                string value= Environment.GetEnvironmentVariable(name);
                items.Add(new EnvVariables()
                {
                    Name = name,
                    Value = value
                });
            }

            return items;
        }

        public string[] GetPath()
        {
            string pathStr = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
            return pathStr.Split(';');
        }

        public bool Add(EnvVariables item)
        {
            try
            {
                Environment.SetEnvironmentVariable(item.Name, item.Value, EnvironmentVariableTarget.Machine);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool AddPath(string path)
        {
            string pathStr = Environment.GetEnvironmentVariable("PATH");
            pathStr += ";";
            pathStr += path;

            return Add(new EnvVariables()
            {
                Name = "PATH",
                Value = pathStr
            });
        }

        public bool Del(EnvVariables item)
        {
            return false;
        }
    }
}
